# import requests
# import json
#
# url = "https://fanqienovel.com/api/author/library/book_list/v0/?page_count=18&page_index=0&gender=-1&category_id=-1&creation_status=-1&word_count=-1&book_type=-1&sort=0&msToken=ZCGiDVwDdcyv_bBFl-p4A3nuJHJZ1JiABQz1K5ykZtiRYTafyMIdxvT970O23zbeRUUvUsBZxjAZbDszvDpdG7gwFBdgSMsn25OsJnWaqW2YHeq1gtpdkOJhGENZJRA%3D&a_bogus=QXlQDO2oMsm11jV2khkz9yGmvOW0YWR%2FgZEztp9nltwl"
# res = requests.get(url)
#
# data_bytes = res.content
# data_str = data_bytes.decode()
# data_json = json.loads(data_str)
#
#
# # data_json = res.json()
#
# print(data_json)
# for i in data_json['data']['book_list']:
#     print(i['book_name'])






from curl_cffi import requests
import os
import json
import time
import shutil
import json
from util.logger import get_looger

root = 'static/番茄小说/'
if os.path.exists(root):
    shutil.rmtree(root)
os.makedirs(root)


logger = get_looger('番茄小说',__file__)
logger.info("爬虫启动......")

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36',
    'Referer': 'https://fanqienovel.com/',
}

with open(f'{root}/番茄小说.txt', 'a', encoding='utf-8') as f:
    for i in range(1,3):
        page = i
        url = f'https://fanqienovel.com/api/author/library/book_list/v0/?page_count=18&page_index={page}&gender=-1&category_id=-1&creation_status=-1&word_count=-1&book_type=-1&sort=0&msToken=CecRO4zk4AD2Jv6CP7eAM8nih-QBIEC_m8W-AXgVXxjIhMjEtKzBQeVu_V9KKkO9DiGGCt9hGPCGnDx5Q1zWW7lcZIYIj0SV1I2GjCw_tbPD1PGv_2LrBWsH0NmbTUM%3D&a_bogus=Yf0EfO2wMsm1af3pX7kz9avm-yy0YW-KgZEPt9GRq0wC'
        logger.info(f'正在爬{page}页')

        response = requests.get( url,headers=headers,impersonate="chrome110" )
        print(response.content.decode('utf-8'))
        content = response.content.decode('utf-8')
        print(type(content))


        content_json = json.loads(content)
        print(content_json)
        print(type(content_json))
        #
        novels = content_json.get('data', {}).get('book_list', [])
        print(novels)

        for novel in novels:
            print("书名:", novel.get('book_name'))
            print("作者:", novel.get('author'))
            print("简介:", novel.get('abstract'))
            print("---")

            f.write(novel.get('book_name', '未知书名'))
            f.write("\n")
            f.write(novel.get('author','无作者'))
            f.write("\n")
            f.write(novel.get('abstract', '无简介'))
            f.write("\n")
            f.write("-"*20+"\n")


logger.info('爬虫结束...')

































# import requests
# url = ('https://fanqienovel.com/api/author/library/book_list/v0/'
#        # '?page_count=18'
#        # '&page_index=0'
#        # '&gender=-1'
#        # '&category_id=-1'
#        # '&creation_status=-1'
#        # '&word_count=-1'
#        # '&book_type=-1'
#        # '&sort=0'
#        # '&msToken=mJC74yjICIGYjpqhpDjYmW_6FpT_xA2qD862Eh_O58PxOEOxBj8X2zimrQzTPC7iMDANBojkO_j4xwXfnEOTLT_Q1G_kC9SRKDzPJT6LFNlHY6XjihDN6qI4X8uQCZc%3D'
#        # '&a_bogus=O6MOgc2oMsm1CffB6hkz9a0mhGR0YW5XgZEPt2opGtqU'
# )
#
# cookies = {
#     's_v_web_id': 'verify_mhd7k31r_6LpvJc1u_AjWG_4rfW_AptI_w0ppBF7k8WRW',
#     'novel_web_id': '7566940642564785707',
#     'Hm_lvt_2667d29c8e792e6fa9182c20a3013175': '1761815664,1761977006',
#     'HMACCOUNT': 'F0C2AC4C8DBC5857',
#     'csrf_session_id': 'f01c4b27b8477e83878e3d4ad129538a',
#     'Hm_lpvt_2667d29c8e792e6fa9182c20a3013175': '1761979870',
#     'ttwid': '1%7CuPIGb2nwgkfEwHS9nroItRh21BP8z5czp49rV0mEgaU%7C1761979873%7C22b1c61f278ef01e36f7dedd1b5cc6e880fdca5720a5ef1fe1738097104ff8cf',
# }
#
# headers = {
#     'accept': 'application/json, text/plain, */*',
#     'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
#     'cache-control': 'no-cache',
#     'pragma': 'no-cache',
#     'priority': 'u=1, i',
#     'referer': 'https://fanqienovel.com/library/all/page_1?sort=hottes',
#     'sec-ch-ua': '"Microsoft Edge";v="141", "Not?A_Brand";v="8", "Chromium";v="141"',
#     'sec-ch-ua-mobile': '?0',
#     'sec-ch-ua-platform': '"Windows"',
#     'sec-fetch-dest': 'empty',
#     'sec-fetch-mode': 'cors',
#     'sec-fetch-site': 'same-origin',
#     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0',
#     # 'cookie': 's_v_web_id=verify_mhd7k31r_6LpvJc1u_AjWG_4rfW_AptI_w0ppBF7k8WRW; novel_web_id=7566940642564785707; Hm_lvt_2667d29c8e792e6fa9182c20a3013175=1761815664,1761977006; HMACCOUNT=F0C2AC4C8DBC5857; csrf_session_id=f01c4b27b8477e83878e3d4ad129538a; Hm_lpvt_2667d29c8e792e6fa9182c20a3013175=1761979870; ttwid=1%7CuPIGb2nwgkfEwHS9nroItRh21BP8z5czp49rV0mEgaU%7C1761979873%7C22b1c61f278ef01e36f7dedd1b5cc6e880fdca5720a5ef1fe1738097104ff8cf',
# }
#
# params = {
#     'page_count': '18',
#     'page_index': '0',
#     'gender': '-1',
#     'category_id': '-1',
#     'creation_status': '-1',
#     'word_count': '-1',
#     'book_type': '-1',
#     'sort': '0',
#     'msToken': 'mJC74yjICIGYjpqhpDjYmW_6FpT_xA2qD862Eh_O58PxOEOxBj8X2zimrQzTPC7iMDANBojkO_j4xwXfnEOTLT_Q1G_kC9SRKDzPJT6LFNlHY6XjihDN6qI4X8uQCZc=',
#     'a_bogus': 'O6MOgc2oMsm1CffB6hkz9a0mhGR0YW5XgZEPt2opGtqU',
# }
#
# response = requests.get(
#     url,
#     params=params,
#     cookies=cookies,
#     headers=headers,
# )
#
# # page = response.json()
# #
# #
# # print(page)
#
#
#
# # 关键新增：先UTF-8解码，再还原Unicode转义
# page = response.content.decode('utf-8')  # 第一步：基础UTF-8解码
# page = page.encode('utf-8').decode('unicode_escape')  # 第二步：核心！还原转义字符
#
# print(page)










# import requests
# import re
#
# def decode_unicode_escape(text):
#     """解码Unicode转义字符"""
#     if isinstance(text, str):
#         try:
#             # 处理\ue开头的Unicode转义字符
#             return text.encode('utf-8').decode('unicode_escape')
#         except:
#             return text
#     return text
#
# def decode_book_data(book):
#     """递归解码书籍数据中的所有字符串"""
#     if isinstance(book, dict):
#         return {key: decode_book_data(value) for key, value in book.items()}
#     elif isinstance(book, list):
#         return [decode_book_data(item) for item in book]
#     elif isinstance(book, str):
#         try:
#             return decode_unicode_escape(book)
#         except:
#             return book
#     return book
#
# url = "https://fanqienovel.com/api/author/library/book_list/v0/?page_count=18&page_index=1&gender=-1&category_id=-1&creation_status=-1&word_count=-1&book_type=-1&sort=0&msToken=GEpPnbiduF8nUoex5AVR6uKGeKalDv9NpluWrKBG1DfzKvejSE15b3eui5HJoYjoXE8nP_OxcCehLmQ-efSVefSc_X3Ctg-gYwGqRTR9jGqljlgx5pzsmw%3D%3D&a_bogus=d7lQXO2oMsm1hE3HRhkz9jGm%2FF80YWRKgZENthqo1zw6"
# res = requests.get(url)
# data = res.json()
#
# print("原始数据状态:", res.status_code)
# print("书籍数量:", len(data['data']['book_list']))
#
# # 解码前5本书的信息
# for i, book in enumerate(data['data']['book_list'][:5]):
#     try:
#         decoded_book = decode_book_data(book)
#         print(f"\n第{i+1}本书:")
#
#         # 安全输出，避免编码错误
#         book_name = decoded_book['book_name']
#         author = decoded_book['author']
#         read_count = decoded_book['read_count']
#         word_count = decoded_book['word_count']
#
#         print("书名:", repr(book_name))
#         print("作者:", repr(author))
#         print("阅读量:", repr(read_count))
#         print("字数:", repr(word_count))
#
#     except Exception as e:
#         print(f"处理第{i+1}本书时出错: {e}")